//
//  WSManager+User.m
//  mUBB
//
//  Created by Cosmin Stirbu on 2/23/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "WSManager+User.h"
#import "Constants.h"
#import "JSONKit.h"
#import "UserHandler.h"


@implementation WSManager (User)

- (Ticket *)signInUserEmail:(NSString *)email withPassword:(NSString *)password userType:(UserType)userType delegate:(id<UserProtocol>)delegate {
    // New ticket
    Ticket *ticket = [[Ticket alloc] initWithId:ticketId++];
    
    // URL
    NSString *url = [[UserDefaultsManager getObjectForKey:WS_USER_SERVICE] stringByAppendingString:@"/signin"];
    
    // Body
    NSMutableDictionary *jsonBody = [NSMutableDictionary new];
    
    [jsonBody setValue:email forKey:USER_EMAIL];
    [jsonBody setValue:password forKey:USER_PASSWORD];
    [jsonBody setValue:[NSNumber numberWithInt:userType] forKey:USER_TYPE];
    
    connectionDelegate = [[UserHandler alloc] initWithDelegate:delegate ticket:ticket];
    
    [self postRequestWithUrl:url body:[jsonBody JSONString] andHeaders:nil];
    
    connectionDelegate = nil;
    
    return ticket;
}

@end
